﻿/*************************************************
 * 
 *  框架级
 *  
 *  数据库通用类 - 执行查询
 * 
 *  create by ternence
 * 
 *  create datetime at 2017-03-17
 *  
 *  西安佳卓信息科技有限公司 ©2017
 * 
 * ***********************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Dapper; 

namespace NF_IDAL
{
    public static class DapperQuery<T> where T : class
    {

        /// <summary>
        /// 执行sql 获取对象集合
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>泛型集合</returns>
        public static IEnumerable<T> DoQueryListT(string sql, object obj)
        {
            using (IDbConnection conn = DapperConn.OpenSqlConnection())
            {
                return conn.Query<T>(sql, obj);
            }
        }

        /// <summary>
        /// 执行查询，返回动态对象
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="obj">动态对象</param>
        /// <returns></returns>
        public static IEnumerable<dynamic> DoQueryListDynamic(string sql, object obj)
        {
            using (IDbConnection conn = DapperConn.OpenSqlConnection())
            {
                return conn.Query<dynamic>(sql, obj);
            }
        }


        /// <summary>
        /// 获取集合对象
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="obj">动态对象</param>
        /// <returns></returns>
        public static List<T> DoQueryList(string sql, object obj)
        {
            using (IDbConnection conn = DapperConn.OpenSqlConnection())
            {
                List<T> listT = conn.Query<T>(sql, obj).ToList();
                return listT;
            }
        }


        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <param name="sql"></param>
        /// <returns>T</returns>
        public static T DoQueryT(string sql, T t)
        {
            using (IDbConnection conn = DapperConn.OpenSqlConnection())
            {
                return conn.Query<T>(sql, t).SingleOrDefault<T>();
            }
        }

        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <param name="sql"></param>
        /// <returns>T</returns>
        public static T DoQueryT(string sql, object obj)
        {
            using (IDbConnection conn = DapperConn.OpenSqlConnection())
            {
                return conn.Query<T>(sql, obj).SingleOrDefault<T>();
            }
        }


        /// <summary>
        /// 获取集合对象
        /// </summary>
        /// <param name="type">链接数据库(为空时链接app库，不为空时链接综合治税库)</param>
        /// <param name="sql">sql语句</param>
        /// <param name="obj">动态对象</param>
        /// <returns></returns>
        public static List<T> DoQueryList(string type,string sql, object obj)
        {
            using (IDbConnection conn = DapperConn.OpenSqlConnection())
            {
                List<T> listT = conn.Query<T>(sql, obj).ToList();
                return listT;
            }
        }

    }
}
